home *** CD-ROM | disk | FTP | other *** search
- /*------------------------------------------------------------------------------
- #
- # Macintosh Developer Technical Support
- #
- # Sample Control Panel Device and INIT Combination
- #
- # Program: INIT - CDEV
- # File: CDEV.c - C Source
- #
- # Copyright © 1990 Apple Computer, Inc.
- # All rights reserved.
- #
- ------------------------------------------------------------------------------*/
- #include "common.h"
-
- #include <Quickdraw.h>
- #include <Files.h>
- #include <Memory.h>
- #include <OSUtils.h>
- #include <TextUtils.h>
- #include <Devices.h>
- #include <Dialogs.h>
- #include <Packages.h>
- #include <Resources.h>
- #include <Retrace.h>
- #include <Packages.h>
-
-
- /*------------------------------------------------------------------------------
- Constants
- ------------------------------------------------------------------------------*/
-
- #define kNoINIT -4048 /* Alert: INIT was not installed */
- #define kCantWritePrefs -4047 /* Alert: error writing preferences */
-
- #define kAboutText1 1
- #define kAboutText2 2
- #define kDebugStrStopButton 3
- #define kDebugStrGoButton 4
- #define kIgnoreButton 5
- #define kManage4 6
-
- /*------------------------------------------------------------------------------
- Var block
- ------------------------------------------------------------------------------*/
-
- typedef struct myVars {
- BusVars BusVarsCopy;
- BusVars *running;
- } myVars, *myVarsPtr, **myVarsHandle;
-
- /*------------------------------------------------------------------------------
- Procedure Prototypes and External Routines
- ------------------------------------------------------------------------------*/
- void ItemHit(int item, int numItems, BusVars *initStorage, DialogPtr dialog);
- void UpdatePrefs(BusVars *initStorage);
- void InitItems( BusVars *initStorage, short numItems, DialogPtr CPDialog);
-
- BusVars *GetCommonStorage(); /* assembly code */
- void InitToPrefs(BusVars *,BusVars *);
-
-
- pascal long cdevEntry(short message, short item, short numItems, short CPanelID,
- EventRecord *theEvent, myVarsHandle storage, DialogPtr CPDialog)
- {
- #pragma unused (CPanelID,theEvent) /* unused formal parameters */
-
- if (message == macDev) {
- return 1; /* shouldn't happen if mach resource is okay */
- }
- if (storage != nil) {
- switch (message) {
- case initDev:
- storage = (myVarsHandle)NewHandle(sizeof(myVars));
- HLock((Handle)storage);
- (*storage)->running = GetCommonStorage();
- if ((*storage)->running) {
- BlockMove((*storage)->running,&(*storage)->BusVarsCopy,sizeof(BusVars));
- InitItems( &(*storage)->BusVarsCopy, numItems, CPDialog);
- HUnlock((Handle)storage);
- }
- else {
- (void) StopAlert(kNoINIT, (ModalFilterProcPtr) nil);
- return(cdevGenErr);
- }
- break;
-
- case closeDev: /* clean up and dispose */
- HLock((Handle)storage);
- UpdatePrefs( &(*storage)->BusVarsCopy );
- HUnlock((Handle)storage);
- DisposHandle((Handle)storage);
- break;
-
- case hitDev: /* handle hit on item */
- HLock((Handle)storage);
- ItemHit(item, numItems, &(*storage)->BusVarsCopy, CPDialog);
- InitToPrefs( &(*storage)->BusVarsCopy, (*storage)->running );
- HUnlock((Handle)storage);
- break;
-
- case activDev: /* activate any needed items */
- case deactivDev: /* deactivate any needed items */
- case updateDev: /* handle any update drawing */
- case keyEvtDev: /* respond to keydown */
- case macDev:
- case undoDev:
- case cutDev:
- case copyDev:
- case pasteDev:
- case clearDev:
- case nulDev:
- break;
-
- }
-
- return ((long) storage);
- } /* cdevStorage != nil */
-
- /*
- ** We get here iff cdevStorage = NIL. Return 0 so that Control Panel
- ** will put up "out of memory" error
- */
- return (0);
- }
-
-
- /* Gets the ControlHandle for the item you want in the dialog box thebox. */
- /* Handy for setting checkboxes and radio buttons */
- ControlHandle SnatchHandle(DialogPtr thebox, short theGetItem)
- {
- short itemtype;
- Rect itemrect;
- Handle thandle;
-
- GetDItem(thebox, theGetItem, &itemtype, &thandle, &itemrect);
- return((ControlHandle)thandle);
- }
-
-
- void InitItems( BusVars *initStorage, short numItems, DialogPtr CPDialog) {
- if ( initStorage->Locked ) {
- DebugStr("\pHm. Seems to be locked. This should never happen.");
- }
- /* Clear the three controls first */
- SetCtlValue(SnatchHandle(CPDialog,numItems+kDebugStrStopButton),0);
- SetCtlValue(SnatchHandle(CPDialog,numItems+kDebugStrGoButton),0);
- SetCtlValue(SnatchHandle(CPDialog,numItems+kIgnoreButton),0);
- switch(initStorage->Behavior)
- {
- case Behavior_Ignore:
- SetCtlValue(SnatchHandle(CPDialog,numItems+kIgnoreButton),1);
- break;
- case Behavior_DebugStrGo:
- SetCtlValue(SnatchHandle(CPDialog,numItems+kDebugStrGoButton),1);
- break;
- case Behavior_DebugStrStop:
- SetCtlValue(SnatchHandle(CPDialog,numItems+kDebugStrStopButton),1);
- break;
- }
- HiliteControl(SnatchHandle(CPDialog,numItems+kManage4),0);
-
- if (initStorage->Do4)
- SetCtlValue(SnatchHandle(CPDialog,numItems+kManage4),1);
- else
- SetCtlValue(SnatchHandle(CPDialog,numItems+kManage4),0);
- }
-
-
- /*------------------------------------------------------------------------------
-
- void UpdatePrefs(CDEVHnd cdevStorage)
-
- Called when the CDEV is being closed. This routine writes to a preferences
- file, using the INIT's public globals as the output buffer. The data is
- written to the data fork; no resources are used at all. If the preferences
- file doesn't exist, it is created. Any errors are reported through an
- Alert saying that error number such-and-such occured.
-
- ------------------------------------------------------------------------------*/
-
- void UpdatePrefs(BusVars *initStorage) {
- OSErr err;
- Str255 errString;
- Handle prefs;
-
- prefs = GetResource('PREF',128);
- if ( prefs ) {
- HLock(prefs);
- BlockMove(initStorage,*prefs,6); /* HARD CODED LENGTH! EVIL! */
- ChangedResource(prefs);
- WriteResource(prefs);
- HUnlock(prefs);
- }
- else {
- err = ResError();
- NumToString(err, errString);
- ParamText(errString, nil, nil, nil);
- (void) StopAlert(kCantWritePrefs, (ModalFilterProcPtr) nil);
- }
- }
-
-
- void ItemHit(int item, int numItems, BusVars *initStorage, DialogPtr dialog) {
- switch( item - numItems ) {
- case kDebugStrStopButton:
- initStorage->Behavior = Behavior_DebugStrStop;
- InitItems(initStorage, numItems, dialog);
- break;
-
- case kDebugStrGoButton:
- initStorage->Behavior = Behavior_DebugStrGo;
- InitItems(initStorage, numItems, dialog);
- break;
-
- case kIgnoreButton:
- initStorage->Behavior = Behavior_Ignore;
- InitItems(initStorage, numItems, dialog);
- break;
-
- case kManage4:
- initStorage->Do4 = !initStorage->Do4;
- InitItems(initStorage, numItems, dialog);
- break;
- }
- }
-
-